package com.tony.ai.ui

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.tony.ai.BaseActivity
import com.tony.ai.R
import com.tony.ai.adapter.BackgroundColorAdapter
import com.tony.ai.adapter.BackgroundImageAdapter
import com.tony.ai.ai.SegmentationReplacer
import com.tony.ai.bean.ColorBean
import com.tony.ai.bean.ImageBean
import com.tony.ai.databinding.ActivitySegmentationBinding

/**
 * 图像分割
 *1.背景替换：替换证件照或视频会议背景。
 *2.背景模糊：突出前景主体。
 *3.虚拟背景：用于视频会议或直播。
 *4.图像编辑：抠图、合成等。
 *5.增强现实：将虚拟对象与真实场景结合。
 */
class SegmentationActivity : BaseActivity() {
    private val TAG = "SegmentationActivity"
    private lateinit var binding: ActivitySegmentationBinding

    //    private var segmenter: Segmenter? = null
    private lateinit var backgroundReplacer: SegmentationReplacer
    private var backgroundBitmap: Bitmap? = null
    private var adapter: BackgroundColorAdapter? = null
    private var colors = ArrayList<ColorBean>()
    private var originalImage: Bitmap? = null
    private var color: Int = Color.WHITE
    private var type: SegmentationReplacer.ReplacerType = SegmentationReplacer.ReplacerType.IMAGE
    private var imageAdapter: BackgroundImageAdapter? = null
    private var images = ArrayList<ImageBean>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySegmentationBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initViews()
    }

    private fun initViews() {
        backgroundBitmap = BitmapFactory.decodeResource(resources, R.drawable.lu)
        // 初始化 BackgroundReplacer
        backgroundReplacer = SegmentationReplacer()
//        initCameraX(binding.viewFinder)
        binding.btnChoicePicture.setOnClickListener {
            choiceImage()
        }
        initRecyclerView()
        initRecyclerViewImage()
        binding.btnImage.setOnClickListener {
            binding.recyclerColors.visibility = View.GONE
            binding.recyclerImages.visibility = View.VISIBLE
        }
        binding.btnColor.setOnClickListener {
            binding.recyclerColors.visibility = View.VISIBLE
            binding.recyclerImages.visibility = View.GONE
        }

    }


    override fun onGetBitmapResult(bitmap: Bitmap?, rotation: Int) {
        type = SegmentationReplacer.ReplacerType.IMAGE
//        replaceBackground()
//        backgroundReplacer.replaceBackgroundImage(this@SegmentationActivity,bitmap,Color.RED,)
    }

    override fun onDestroy() {
        super.onDestroy()
        backgroundReplacer.release()
    }

    override fun onImageResult(path: String?) {
        originalImage = BitmapFactory.decodeFile(path)
        type = SegmentationReplacer.ReplacerType.IMAGE
        backgroundReplacer.replaceBackgroundImage(
            this@SegmentationActivity, originalImage, Color.RED, binding.cbBlur.isChecked,binding.cbKouTu.isChecked,
            R.drawable.image_1
        ) { resultBitmap ->
            // 显示替换背景后的图像
            binding.ivReplaceBackground.setImageBitmap(resultBitmap)
        }
    }


    private fun initRecyclerView() {
        colors.add(ColorBean(Color.WHITE))
        colors.add(ColorBean(Color.RED))
        colors.add(ColorBean(Color.BLACK))
        colors.add(ColorBean(Color.BLUE))
        colors.add(ColorBean(Color.GRAY))
        colors.add(ColorBean(Color.GREEN))
        colors.add(ColorBean(Color.DKGRAY))
        colors.add(ColorBean(Color.LTGRAY))
        colors.add(ColorBean(Color.MAGENTA))
        colors.add(ColorBean(Color.YELLOW))
        colors.add(ColorBean(Color.CYAN))
        val layoutManager =
            LinearLayoutManager(
                this@SegmentationActivity,
                LinearLayoutManager.HORIZONTAL,
                false
            ) //第二个参数为网格的列数
        binding.recyclerColors.layoutManager = layoutManager
        adapter = BackgroundColorAdapter { pos, item ->
            backgroundReplacer.replaceBackgroundColor(
                originalImage!!,
                backgroundBitmap!!,
                binding.cbKouTu.isChecked,
                item.color
            ) { resultBitmap ->
                // 显示替换背景后的图像
                binding.ivReplaceBackground.setImageBitmap(resultBitmap)
            }
        }
        adapter?.setData(colors)
        binding.recyclerColors.adapter = adapter
    }

    private fun initRecyclerViewImage() {
        images.add(ImageBean(R.drawable.image_1))
        images.add(ImageBean(R.drawable.image_2))
        images.add(ImageBean(R.drawable.image_3))
        images.add(ImageBean(R.drawable.image_4))
        images.add(ImageBean(R.drawable.image_5))
        images.add(ImageBean(R.drawable.image_6))
        images.add(ImageBean(R.drawable.image_7))
        images.add(ImageBean(R.drawable.image_8))
        images.add(ImageBean(R.drawable.image_9))
        images.add(ImageBean(R.drawable.image_10))
        images.add(ImageBean(R.drawable.image_11))
        images.add(ImageBean(R.drawable.image_12))
        images.add(ImageBean(R.drawable.image_13))
        images.add(ImageBean(R.drawable.image_14))
        val layoutManager =
            LinearLayoutManager(
                this@SegmentationActivity,
                LinearLayoutManager.HORIZONTAL,
                false
            ) //第二个参数为网格的列数
        binding.recyclerImages?.layoutManager = layoutManager
        imageAdapter = BackgroundImageAdapter { pos, item ->
            backgroundReplacer.replaceBackgroundImage(
                this@SegmentationActivity, originalImage,
                Color.RED, binding.cbBlur.isChecked, binding.cbKouTu.isChecked,item.resId
            ) { resultBitmap ->
                // 显示替换背景后的图像
                binding.ivReplaceBackground.setImageBitmap(resultBitmap)
            }
        }
        imageAdapter?.setData(images)
        binding.recyclerImages.adapter = imageAdapter
    }
}